export const copy = async (text: string) => {
  const $textarea = document.createElement('textarea');
  $textarea.value = text;
  document.body.appendChild($textarea);
  $textarea.select();
  document.execCommand('Copy'); // 执行浏览器复制命令
  $textarea.style.display = 'none';
  document.body.removeChild($textarea);
}

/**
 * @description: 通过URL下载文件
 * @param {string} url 下载地址
 * @param {*} name 下载文件名称
 */
export const downloadFileByUrl = (url: string, name = `${Date.now()}`) => {
  const a = document.createElement('a');
  a.setAttribute('href', url);
  a.setAttribute('download', name);
  a.setAttribute('target', '_blank');
  const clickEvent = document.createEvent('MouseEvents');
  clickEvent.initEvent('click', true, true);
  a.dispatchEvent(clickEvent);
};

/**
 * @description: 切换主题
 * @return {*}
 */

export const toggleTheme = (theme:string) => {
  const html = document.querySelector('html') as HTMLElement;
  html.setAttribute('data-theme', theme);
};
